package com.teach.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teach.dto.CourseSection;
import com.teach.entity.ResponseEntity;
import com.teach.entity.TbCourseSection;
import com.teach.entity.TbMediaAssets;
import com.teach.mapper.TbMediaAssetsMapper;
import com.teach.mapper.TbUserMapper;
import com.teach.service.TbCourseSectionService;
import com.teach.mapper.TbCourseSectionMapper;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class TbCourseSectionServiceImpl extends ServiceImpl<TbCourseSectionMapper, TbCourseSection>
    implements TbCourseSectionService{
    @Resource
    TbCourseSectionMapper tbCourseSectionMapper;
    @Resource
    TbUserMapper tbUserMapper;
    @Resource
    TbMediaAssetsMapper tbMediaAssetsMapper;

    @Override
    public ResponseEntity getAll(CourseSection courseSection) {
        List<Map> mapList = tbCourseSectionMapper.selectByBaseId(courseSection.getBaseId());
        return new ResponseEntity(mapList,200,"查询成功",true);
    }

    @Override
    public ResponseEntity sectionAdd(CourseSection courseSection, String token) {
        Boolean flag=false;
        List<Map> list=new ArrayList<>();
        //解析token
        Claims claims = Jwts.parser().setSigningKey("huangyujie").parseClaimsJws(token).getBody();
        int uid = Integer.parseInt(claims.getId());
        //根据登录人的id判断是否有权限
        List<Map> mapList = tbUserMapper.selectByUid(uid);
        for (Map map : mapList) {
            //二级名称
            String fname = (String) map.get("fname");
            int indexOf = fname.indexOf("分类管理");
            if(indexOf !=-1){
                //根据登录人的id和二级名称查询三级的名称
                list = tbUserMapper.selectConunt(uid,fname);
            }
        }
        //判断是否有添加的权限
        for (Map map : list) {
            //三级名称
            String gname= (String) map.get("gname");
            //判断是否有添加权限
            if((gname.indexOf("添加"))!=-1){
                flag=true;
            }
        }
        if(flag){
            TbCourseSection tbCourseSection = new TbCourseSection();
            tbCourseSection.setBaseId(courseSection.getBaseId());
            tbCourseSection.setSectionId(courseSection.getSectionId());
            tbCourseSection.setSectionName(courseSection.getSectionName());
            //添加章节表
            tbCourseSectionMapper.insert(tbCourseSection);
            /*//根据添加的章节表的主键添加媒资表
            TbMediaAssets tbMediaAssets = new TbMediaAssets();
            tbMediaAssets.setVideoLink(url);
            tbMediaAssets.setDuration("30");
            tbMediaAssets.setSectionId(tbCourseSection.getId());
            tbMediaAssets.setSectionName(tbCourseSection.getSectionName());
            tbMediaAssetsMapper.insert(tbMediaAssets);*/
        }
        return new ResponseEntity(null,200,"添加成功",true);
    }

    @Override
    public ResponseEntity delById(Integer id, String token) {

        return null;
    }

    @Override
    public ResponseEntity findById(Integer id) {
        TbCourseSection tbCourseSection = tbCourseSectionMapper.selectById(id);
        return new ResponseEntity(tbCourseSection,200,"回显成功",true);
    }

    @Override
    public ResponseEntity updateSecation(CourseSection courseSection, String token) {
        Boolean flag=false;
        List<Map> list=new ArrayList<>();
        //解析token
        Claims claims = Jwts.parser().setSigningKey("huangyujie").parseClaimsJws(token).getBody();
        int uid = Integer.parseInt(claims.getId());
        //根据登录人的id判断是否有权限
        List<Map> mapList = tbUserMapper.selectByUid(uid);
        for (Map map : mapList) {
            //二级名称
            String fname = (String) map.get("fname");
            int indexOf = fname.indexOf("分类管理");
            if(indexOf !=-1){
                //根据登录人的id和二级名称查询三级的名称
                list = tbUserMapper.selectConunt(uid,fname);
            }
        }
        //判断是否有修改的权限
        for (Map map : list) {
            //三级名称
            String gname= (String) map.get("gname");
            //判断是否有删除权限
            if((gname.indexOf("修改"))!=-1){
                flag=true;
            }
        }
        if(flag){
            TbCourseSection tbCourseSection = new TbCourseSection();
            tbCourseSection.setBaseId(courseSection.getBaseId());
            tbCourseSection.setSectionId(courseSection.getSectionId());
            tbCourseSection.setSectionName(courseSection.getSectionName());
            //修改章节表
            tbCourseSectionMapper.updateById(tbCourseSection);
        }
        return new ResponseEntity(null,200,"修改成功",true);
    }
}




